In [1]:
"This is a string!!!"
Out[1]:
In [2]:
'This is also a string!!!'
Out[2]:
In [3]:
"This string contains single 'quotation' marks!!!"
Out[3]:
In [4]:
'This string contains double "quotation" marks!!!'
Out[4]:
In [5]:
7
Out[5]:
In [6]:
42
Out[6]:
Integers support all of the operations you would expect:
+
Addition -
Subtraction*
Multiplication/
Division%
Modulo
In [7]:
5 + 3
Out[7]:
In [8]:
5 - 3
Out[8]:
In [9]:
5 * 3
Out[9]:
In [10]:
5 / 3 # This is weird. More on this later.
Out[10]:
In [11]:
5 % 3 # The remainder of 5 / 3
Out[11]:
In [12]:
7.
Out[12]:
In [13]:
42.
Out[13]:
In [14]:
1.5
Out[14]:
Floats support the same operations as integers.
In [15]:
3/4
Out[15]:
Mathematically this should be 0.75, but as the computation is using integers the result rounds down. Using floats on the other hand yields more accurate result.
In [16]:
3./4.
Out[16]:
Note that just using floats isn't enough to give you a mathematically correct answer. For example
In [17]:
0.1 + 0.2
Out[17]:
On a computer, floating-point numbers are represented using a finite number of bits (ie. memory). This means that not every real number has a floating-point number associated with it, meaning that computers need to perform rounding to represent floating-point numbers and the results of computations on floating-point numbers. This is a source of error that needs to be taken into account when performing numerical computations.
For more details look here.
In [18]:
x = 1
y = 2.
z = x + y
print(type(x))
print(type(y))
print(type(z))
In [19]:
3./4
Out[19]:
In [20]:
True
Out[20]:
In [21]:
False
Out[21]:
They support the kinds of operations you would expect from boolean logic.
In [22]:
True and False
Out[22]:
In [23]:
True or False
Out[23]:
In [24]:
not True
Out[24]:
Booleans can be generated from equality comparisons.
In [25]:
1 == 2 # Check equality.
Out[25]:
In [26]:
1 != 2 # Checks non-equality
Out[26]:
In [27]:
2 > 4 # Size comparison
Out[27]:
In [28]:
"zebra" == "zebra" # Comparing strings
Out[28]:
In [29]:
(1, 2, 3) == (1, 2, 3) # Comparing tuples
Out[29]: